package com.shizhanzhe.gxb.home.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.shizhanzhe.gxb.R;

import java.util.List;

/**
 * Created by YJ on 2019/1/25.
 */
public class HistoryAdapter extends ArrayAdapter<String> {

    private int resourceid;
    private UserItemClickListener mListener;

    public void setListener(UserItemClickListener listener) {
        mListener = listener;
    }

    public HistoryAdapter(@NonNull Context context, int resource, @NonNull List<String> objects) {
        super(context, resource, objects);
        resourceid = resource;
    }

    @SuppressLint("SetTextI18n")
    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        View view;
        ViewHolder viewHolder;
        if (convertView == null){
            view = LayoutInflater.from(getContext()).inflate(resourceid,parent,false);
            viewHolder = new ViewHolder();
            viewHolder.tvName = view.findViewById(R.id.tv_name);
            viewHolder.ivClose = view.findViewById(R.id.iv_close);
            view.setTag(viewHolder);
        } else {
            view = convertView;
            viewHolder = (ViewHolder) view.getTag();
        }
        final String item = getItem(position);
        viewHolder.tvName.setText(item);
        viewHolder.ivClose.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mListener.itemClick(item);
            }
        });
        return view;
    }

    class ViewHolder {
        TextView tvName;
        ImageView ivClose;
    }
    public interface UserItemClickListener{
        void itemClick(String bean);
    }
}
